<?php
define("HIGH", 1);
define("LOW",  0);
define("INPUT", 0);
define("OUTPUT",  1);
define("PINMODE",     112);
define("DIGITALWRITE",115);
define("DIGITALREAD", 100);
define("ANALOGWRITE", 119);
define("ANALOGREAD",   97);
define("DIGWRIALL",   120);
define("PINMODALL",   121);
define("STEPPER",     122);
define("ONEWIRE",     123);
define("HEARTBEAT",    91);

$g_ddrb    = array();
$g_ddrd    = array();
$g_ddrc    = array();
$g_portb   = array();
$g_portd   = array();
$g_portc   = array();
$g_pinb    = array();
$g_pind    = array();
$g_pinc    = array();
$g_analog  = array(); // Read values
$g_digital = array(); // Read values
$g_hb      = array(); // Heartbeat
$g_oneWire = array(); 

$g_arduino = array(); // status of connection to Arduino 1=no 2=yes
//============================
function colorStatus($id)
{
	$day    = date('d');
    $month  = date('m');
    $year   = date('Y');
    $sec    = date('s');
    $min    = date('i');
    $hour   = date('H');
    $color = 2;
	$file = $id.'.hb';
	$in  = fopen($file,"r");
    if($in)
	{
	  while (!feof($in))
	    {
	      $row  = fgets($in);
	      sscanf($row, "%d %d %d %d %d %d", $yyear, $mmonth,$dday,$hhour,$mmin,$ssec);
	      if($yyear == $year && $mmonth == $month && $dday == $day)
	      {
			  $temp1 = $hhour*3600+$mmin*60+$ssec;
			  $temp2 = $hour*3600+$min*60+$sec;	
			  if($temp1 > $temp2 &&	$temp1 - $temp2	< 11)$color = 1;  
		  }
        }
     }
     if($color == 1) echo("<h1>ONLINE</h1>");
     else
     echo("<h1>OFFLINE $temp1, $temp2</h1>");
     return($color);
}
//============================
function stepTime($value,$delta)
{
  $value  = $value + $delta;
  $value = sprintf("%02d",$value);
  return($value);
}
//============================
function readBit($key,$xx)
{
  global $g_ddrb,$g_ddrd,$g_ddrc;
  global $g_portb,$g_portd,$g_portc;
  global $g_pinb,$g_pind,$g_pinc;
  // PIN State of INPUT pins  		(read)
  // PORT HIGH or LOW 				(write/read)
  // DDR  PinMode - INPUT or OUTPUT 	(write/read)
  // B (digital pin 8 to 13)
  // C (analog input pins 0 to 5)
  // D (digital pins 0 to 7)
  $values = array(1, 2, 4, 8,16,32,64,128);
  $ii= 0;
  //echo("xx=$xx<br>");
  foreach ($values as $value) 
    {
      $result = $value & $xx; 
      $bit = $result >> $ii;
      if($key=='DDRB') $g_ddrb[$ii] = $bit;
      if($key=='DDRC') $g_ddrc[$ii] = $bit;
      if($key=='DDRD') $g_ddrd[$ii] = $bit;
        
      if($key=='PORTB') $g_portb[$ii] = $bit;
      if($key=='PORTC') $g_portc[$ii] = $bit;
      if($key=='PORTD') $g_portd[$ii] = $bit;
        
      if($key=='PINB') $g_pinb[$ii] = $bit;
      if($key=='PINC') $g_pinc[$ii] = $bit;
      if($key=='PIND') $g_pind[$ii] = $bit;
      // echo("$bit");
      $ii++;
    }
}
//============================
function getStatus($id) 
//============================
{
  global $g_ddrb,$g_ddrd,$g_ddrc;
  global $g_portb,$g_portd,$g_portc;
  global $g_pinb,$g_pind,$g_pinc;
  global $g_analog,$g_digital,$g_hb;
  global $g_oneWire,$g_arduino;
 
  $g_oneWire[$id][0] = 0;

  $file = 'status-'.$id.'.arh';
  if (file_exists($file)) 
    {
      $in  = fopen($file,"r");
      if($in)
	{
	  while (!feof($in))
	    {
	      $row  = fgets($in);
	      //echo($row);
	      sscanf($row, "%s %d", $key, $xx);
	      if($key=='ARDUINO') $g_arduino[$id] = $xx;
	      if($key=='HB') $g_hb[$id] = $xx;
	      if($key=='A0') $g_analog[$id][0] = $xx;
	      if($key=='A1') $g_analog[$id][1] = $xx;
	      if($key=='A2') $g_analog[$id][2] = $xx;
	      if($key=='A3') $g_analog[$id][3] = $xx;
	      if($key=='A4') $g_analog[$id][4] = $xx;
	      if($key=='A5') $g_analog[$id][5] = $xx;
	      if($key == 'DDRD'  || $key == 'DDRB'  || $key == 'DDRC')  readBit($key,$xx);
	      if($key == 'PORTD' || $key == 'PORTB' || $key == 'PORTC') readBit($key,$xx);
	      if($key == 'PIND'  || $key == 'PINB'  || $key == 'PINC')  readBit($key,$xx);
	      if($key=='W0') $g_oneWire[$id][0] = $xx;
	      if($key=='W1') $g_oneWire[$id][1] = $xx;
	      if($key=='W2') $g_oneWire[$id][2] = $xx;
	      if($key=='W3') $g_oneWire[$id][3] = $xx;
	      if($key=='W4') $g_oneWire[$id][4] = $xx;
	      if($key=='W5') $g_oneWire[$id][5] = $xx;
	      //echo("<br>"); 
	    }
	  for($ii=0;$ii<8;$ii++)$g_digital[$id][$ii]   = $g_pind[$ii];
	  for($ii=0;$ii<6;$ii++)$g_digital[$id][8+$ii] = $g_pinb[$ii];
	  fclose($in);
	}
    }
  else
    echo("No Status file for unit: $id<br>");
}
//============================
function showConnectionStatus($id)
{
  $file = $id.'.ip';
  if (file_exists($file)) 
    {
      $in  = fopen($file,"r");
      if($in)
	{
	  while (!feof($in))
	    {
	      $row  = fgets($in);
            }
         }
     }
}
//============================
function showPinStatus($id)

{
  global $g_ddrb,$g_ddrd,$g_ddrc;
  global $g_portb,$g_portd,$g_portc;
  global $g_pinb,$g_pind,$g_pinc;
  global $g_analog,$g_digital,$g_hb;
  global $g_oneWire,$g_arduino;
    
  $ctx[0] = "L"; $ctx[1] = "H";
  $cty[0] = "I"; $cty[1] = "O";
  // PIN State of INPUT pins  		(read)
  // PORT HIGH(1) or LOW(0) 				(write/read)
  // DDR  PinMode - INPUT(0) or OUTPUT(1) 	(write/read)
  // B (digital pin 8 to 13)
  // C (analog input pins 0 to 5)
  // D (digital pins 0 to 7)
  echo("<div>");
  echo("<div><table border=1>");
  echo("<tr><th>DIGITAL PIN</th>");
  for($ii=0;$ii<14;$ii++)echo("<th>$ii</th>");
  echo("</tr>");
  echo("<tr><th>PINMODE</th>");
  for($ii=0;$ii<8;$ii++){$temp = $g_ddrd[$ii];$temp = $cty[$temp];echo("<th>$temp</th>");}
  for($ii=0;$ii<6;$ii++){$temp = $g_ddrb[$ii];$temp = $cty[$temp];echo("<th>$temp</th>");}
  echo("</tr>");
  echo("<tr><th>State of INPUT pins</th>");
  for($ii=0;$ii<8;$ii++){$temp = $g_pind[$ii];$temp = $ctx[$temp];echo("<th>$temp</th>");}
  for($ii=0;$ii<6;$ii++){$temp = $g_pinb[$ii];$temp = $ctx[$temp];echo("<th>$temp</th>");}
  echo("</tr>");
  echo("<tr><th>HIGH or LOW </th>");
  for($ii=0;$ii<14;$ii++){$temp = $g_digital[$id][$ii];$temp = $ctx[$temp];echo("<th>$temp</th>");}
  echo("</tr>");
  echo("</table></div>");
  echo("<div><table border=1>");
  echo("<tr><th>ANALOG PIN</th>");
  for($ii=0;$ii<6;$ii++)echo("<th>$ii</th>");
  echo("</tr>");
  echo("<tr><th>Values:</th>");     
  for($ii=0;$ii<=5;$ii++)
    {
      $temp = $g_analog[$id][$ii];
      echo("<th>$temp</th>");
    }
  echo("</tr>");
  echo("<tr><th>OneWire</th>");     
  for($ii=1;$ii<=$g_oneWire[$id][0];$ii++)
    {
      $temp = $g_oneWire[$id][$ii];
      echo("<th>$temp</th>");
    }
  echo("</tr>");
  echo("<tr><th>Connection Status</th>");     
  $temp = $g_arduino[$id];
  echo("<th>$temp</th>");
  echo("</tr>");
  echo("</table></div>");
  echo("</div>");
    
}
//======================================================================
function createPDE_analog($id,$year,$month,$day,$a0,$a1,$a2,$a3,$a4,$a5)
{

  $inpFile = 'data-'.$id.'-'.$year.'-'.$month.'-'.$day.'.arh';
  $file = 'analogGraph-'.$id.'.pde';
  $f = fopen($file,'w'); 

  $content = "String[] lines;
  int gH = 500, gW = 1000;
  int i,temp,maxVal=0;
  int xstart=0;
  int xend=24*3600;
  float floor = 300.;
  float roof = 700.;
  int[] a = new int[6];
  float[] ot = new float[6];
  float[] op = new float[6];
  float[] red   = new float[6];
  float[] green = new float[6];
  float[] blue  = new float[6];
  int[] curves = new int[6];

  void setup() 
  {

     curves[0] = $a0;
     curves[1] = $a1;
     curves[2] = $a2;
     curves[3] = $a3;
     curves[4] = $a4;
     curves[5] = $a5;

     red[0] =   0.; green[0] =   0.; blue[0] =   0.;
     red[1] =   0.; green[1] =  50.; blue[1] =   0.;
     red[2] =   0.; green[2] =   0.; blue[2] =  50.;
     red[3] =  50.; green[3] =  50.; blue[3] =   0.;
     red[4] =   0.; green[4] =  50.; blue[4] =  50.;
     red[5] =  50.; green[5] =  50.; blue[5] =  50.;

     size(gW, gH);
     background(200);
     fill(255,0,0);
     stroke(150,150,150);

     // Vertical lines
     for(i=1;i<24;i++)
     {
      temp = i/24*gW;
      line(temp,gH,temp,15);
      text(i,temp,20);
     }

     // Horizontal lines
     for(i=1;i<=int((roof-floor)/100);i++)
     {
       float yp = float(i*100)/((roof-floor)/gH);
       yp=gH-yp;
       line(0,yp,gW,yp);
       text(i*100+floor,0,yp);
     }

     lines = loadStrings(\"$inpFile\");
     text(\"Analog\",10,10);
     int j=0,k=0,h=0,m=0,s=0;
     int t=0,t_prev=0;

     for(i=0;i<lines.length;i++)
     {
        String s2 = trim(lines[i]);
        String[] pieces = splitTokens(s2, \" \");
        if (pieces.length > 8) 
        {
           h    = int(pieces[0]);
           m    = int(pieces[1]);
           s    = int(pieces[2]);
           a[0] = int(pieces[3]);
           a[1] = int(pieces[4]);
           a[2] = int(pieces[5]);
           a[3] = int(pieces[6]);
           a[4] = int(pieces[7]);
           a[5] = int(pieces[8]);
          // w[1] = int(pieces[9]);
          // w[2] = int(pieces[10]);
          // w[3] = int(pieces[11]);
        }

        t_prev = t;
        t = h*3600+m*60+s;
        //println(t);
        for(k=0;k<6;k++)
        {
         if(curves[k] == 1)
         {
           stroke(red[k],green[k],blue[k]);
           if(t > xstart && t < xend)
           {
              float y = (a[k]-floor)/(roof-floor)*gH;    
              float x = (float)(t-xstart)/(xend - xstart)*gW;
              y = gH-y;
              line(ot[k],op[k],x,y);
              ot[k]=x;op[k]=y;
           }
         }
        }
      }
  }


  void draw() 
  {
  }
  ";

  fwrite($f,$content,strlen($content)); 
  fclose($f); 
}
//======================================================================
function createPDE_1by1($config)
{
  $graphId = $config[graphId];
  $year1   = $config[year1];
  $month1  = $config[month1];
  $day1    = $config[day1];
  $id1     = $config[id1];
  $g1      = $config[g1];
  $s1      = $config[s1];
  $text1   = $config[text1];
  $floor   = $config[floor];
  $roof    = $config[roof];
  $gH      = $config[gH];
  $gW      = $config[gW];
  $hr      = $config[hr];
  $period  = $config[period];

  $inpFile1 = 'data-'.$id1.'-'.$year1.'-'.$month1.'-'.$day1.'.arh';

  $file = 'graph1by1-'.$graphId.'.pde';
  $f = fopen($file,'w'); 

  $content = 
  "
  String[] lines;
  int gH = $gH, gW = $gW;
  int i,temp,maxVal=0;
  int xstart=0;
  int xend=24*3600;
  float floor =$floor;
  float roof = $roof;
  int a;
  float ot,op,red,green,blue;

  void setup() 
  {
     String s2;
     String[] pieces;
     float x,y;
     int j=0,k=0,h=0,m=0,s=0;
     int t=0,t_prev=0;
     int a1=0,a2=0,a3=0,a4=0,a5=0,b1,b2,b3;
     int t1=0,t2=0,freq;

     size(gW, gH);
     background(200);
     fill(255,0,0);
     stroke(150,150,150);


     // Vertical lines
     for(i=1;i<24;i++)
     {
      temp = i/24*gW;
      line(temp,gH,temp,15);

      text(i,temp,20);
     }

     // Horizontal lines
     for(i=1;i<=int((roof-floor)/$hr);i++)

     {
       float yp = float(i*$hr)/((roof-floor)/gH);
       yp=gH-yp;
       line(0,yp,gW,yp);

       text(i*$hr+floor,0,yp);
     }

     //======================================
     // Curve 1
     //======================================
     lines = loadStrings(\"$inpFile1\");

     red = 0.; green = 0.; blue = 0.;
     stroke(red,green,blue);
     fill(red,green,blue);
     text(\"$text1\",10,10);

     t=0;
     t_prev=0;
     ot=0.0; op=0.0;

     for(i=0;i<lines.length;i++)
     {

        s2     = trim(lines[i]);
        pieces = splitTokens(s2, \" \");
        if (pieces.length > 8) 
        {
           h    = int(pieces[0]);
           m    = int(pieces[1]);
           s    = int(pieces[2]);
           a1 = a2;
           a2 = a3;
           a3 = a4;
           //a4 = a5;
           a    = int(pieces[$g1]);
           a4 = a;
        }
        //b1 = a1 + a2; b2 = a2 + a3; b3 = a3 + a4;
        b2 = b1; b1 = a4 - a3;  

        t_prev = t;
        t = h*3600+m*60+s;

        if(t > xstart && t < xend)
        {
              y = (float)a/$s1;
              y = (y-floor)/(roof-floor)*gH;    
              x = (float)(t-xstart)/(xend - xstart)*gW;
              y = gH-y;
              line(ot,op,x,y);
              ot=x;op=y;
              if($period == 1)
              {
                //if(b1<=b2 && b2>=b3)
                if(b1 > 0 && b2 < 0) // minimum
                {
				  stroke(0,255,0);ellipse(x, y, 5, 5);
				  stroke(red,green,blue);
				  t1 = t2; t2 = t; freq = int((t2-t1)/60); //minutes
				  text(freq,x,y);
		        }
		        if(b1 < 0 && b2 > 0) // maximum
                {
				  stroke(0,0,255);ellipse(x, y, 5, 5);
				  stroke(red,green,blue);
				  t1 = t2; t2 = t; freq = int((t2-t1)/60); //minutes
				  text(freq,x,y);
		        }
		      }
        }
     }
    //=================================================
  } // setup



  void draw() 
  {
  }
  ";

  fwrite($f,$content,strlen($content)); 
  fclose($f); 
  return($file);
}
//======================================================================
function createPDE_2by2($config)
{
  $graphId = $config[graphId];
  $year1  = $config[year1];
  $month1 = $config[month1];
  $day1   = $config[day1];

  $year2  = $config[year2];
  $month2 = $config[month2];
  $day2   = $config[day2];

  $id1 = $config[id1];
  $id2 = $config[id2];

  $g1 = $config[g1];
  $g2 = $config[g2];

  $s1 = $config[s1];
  $s2 = $config[s2];

  $text1 = $config[text1];
  $text2 = $config[text2];

  $floor = $config[floor];
  $roof  = $config[roof];

  $gH = $config[gH];
  $gW = $config[gW];

  $hr = $config[hr];

  $inpFile1 = 'data-'.$id1.'-'.$year1.'-'.$month1.'-'.$day1.'.arh';
  $inpFile2 = 'data-'.$id2.'-'.$year2.'-'.$month2.'-'.$day2.'.arh';

  $file = 'graph2by2-'.$graphId.'.pde';
  $f = fopen($file,'w'); 

  $content = 
  "
  String[] lines;
  int gH = $gH, gW = $gW;
  int i,temp,maxVal=0;
  int xstart=0;
  int xend=24*3600;
  float floor =$floor;
  float roof = $roof;
  int a;
  float ot,op,red,green,blue;

  void setup() 
  {
     String s2;
     String[] pieces;
     float x,y;
     int j=0,k=0,h=0,m=0,s=0;
     int t=0,t_prev=0;

     size(gW, gH);
     background(200);
     fill(255,0,0);
     stroke(150,150,150);


     // Vertical lines
     for(i=1;i<24;i++)
     {
      temp = i/24*gW;
      line(temp,gH,temp,15);

      text(i,temp,20);
     }

     // Horizontal lines
     for(i=1;i<=int((roof-floor)/$hr);i++)

     {
       float yp = float(i*$hr)/((roof-floor)/gH);
       yp=gH-yp;
       line(0,yp,gW,yp);

       text(i*$hr+floor,0,yp);
     }

     //======================================
     // Curve 1
     //======================================
     lines = loadStrings(\"$inpFile1\");

     red = 0.; green = 0.; blue = 0.;
     stroke(red,green,blue);
     fill(red,green,blue);
     text(\"$text1\",10,10);

     t=0;
     t_prev=0;
     ot=0.0; op=0.0;

     for(i=0;i<lines.length;i++)
     {

        s2     = trim(lines[i]);
        pieces = splitTokens(s2, \" \");
        if (pieces.length > 8) 
        {
           h    = int(pieces[0]);
           m    = int(pieces[1]);
           s    = int(pieces[2]);
           a    = int(pieces[$g1]);
        }

        t_prev = t;
        t = h*3600+m*60+s;

        if(t > xstart && t < xend)
        {
              y = (float)a/$s1;
              y = (y-floor)/(roof-floor)*gH;    
              x = (float)(t-xstart)/(xend - xstart)*gW;

              y = gH-y;
              line(ot,op,x,y);
              ot=x;op=y;
        }

    }
     //======================================
     // Curve 2
     //======================================
     lines = loadStrings(\"$inpFile2\");

     red = 200.; green = 10.; blue = 50.;
     stroke(red,green,blue);
     fill(red,green,blue);
     text(\"$text2\",60,10);

     t=0;
     t_prev=0;
     ot=0.0; op=0.0;

     for(i=0;i<lines.length;i++)
     {
        s2     = trim(lines[i]);
        pieces = splitTokens(s2, \" \");
        if (pieces.length > 8) 
        {
           h    = int(pieces[0]);
           m    = int(pieces[1]);
           s    = int(pieces[2]);
           a    = int(pieces[$g2]);
        }

        t_prev = t;
        t = h*3600+m*60+s;

        if(t > xstart && t < xend)
        {
              y = (float)a/$s2;
              y = (y-floor)/(roof-floor)*gH;    
              x = (float)(t-xstart)/(xend - xstart)*gW;
              y = gH-y;
              line(ot,op,x,y);
              ot=x;op=y;
        }
    }
    //=================================================
  } // setup



  void draw() 
  {
  }
  ";

  fwrite($f,$content,strlen($content)); 
  fclose($f); 
  return($file);
}
//======================================================================
function createPDE_2by2diff($config)
{
  $graphId = $config[graphId];
  $year1  = $config[year1];
  $month1 = $config[month1];
  $day1   = $config[day1];

  $year2  = $config[year2];
  $month2 = $config[month2];
  $day2   = $config[day2];

  $id1 = $config[id1];
  $id2 = $config[id2];

  $g1 = $config[g1];
  $g2 = $config[g2];

  $s1 = $config[s1];
  $s2 = $config[s2];

  $text1 = $config[text1];
  $text2 = $config[text2];

  $floor = $config[floor];
  $roof  = $config[roof];

  $gH = $config[gH];
  $gW = $config[gW];

  $hr = $config[hr];

  $inpFile1 = 'data-'.$id1.'-'.$year1.'-'.$month1.'-'.$day1.'.arh';
  $inpFile2 = 'data-'.$id2.'-'.$year2.'-'.$month2.'-'.$day2.'.arh';

  $file = 'graph2by2diff-'.$graphId.'.pde';
  $f = fopen($file,'w'); 

  $content = 
  "
  String[] lines;
  int gH = $gH, gW = $gW;
  int i,temp,maxVal=0;
  int xstart=0;
  int xend=24*3600;
  float floor =$floor;
  float roof = $roof;
  int a;
  float ot,op,red,green,blue;

  void setup() 
  {
     String s2;
     String[] pieces;
     float x,y;
     int j=0,k=0,h=0,m=0,s=0;
     int t=0,t_prev=0;
     int[] data1 = new int[86401];

     size(gW, gH);
     background(200);
     fill(255,0,0);
     stroke(150,150,150);

     // Vertical lines
     for(i=1;i<24;i++)
     {
      temp = i/24*gW;
      line(temp,gH,temp,15);
      text(i,temp,20);
     }

     // Horizontal lines
     for(i=1;i<=int((roof-floor)/$hr);i++)
     {
       float yp = float(i*$hr)/((roof-floor)/gH);
       yp=gH-yp;
       line(0,yp,gW,yp);
       text(i*$hr+floor,0,yp);
     }

     //======================================
     // Read Data from file 1
     //======================================
     lines = loadStrings(\"$inpFile1\");
     t_prev = 0;
     for(i=0;i<lines.length;i++)
     {
        s2     = trim(lines[i]);
        pieces = splitTokens(s2, \" \");
        h      = int(pieces[0]);
        m      = int(pieces[1]);
        s      = int(pieces[2]);
        a      = int(pieces[$g1]);
 
        t_prev = t;
        t      = h*3600+m*60+s;
        for(j=t_prev+1;j<=t;j++)data1[j] = a;
     }

     //======================================
     // Second data and the diff graph 
     //======================================
     lines = loadStrings(\"$inpFile2\");

     red = 200.; green = 10.; blue = 50.;
     stroke(red,green,blue);
     fill(red,green,blue);
     text(\"$text1\",60,10);

     t=0;
     ot=0.0; op=0.0;

     for(i=0;i<lines.length;i++)
     {
        s2     = trim(lines[i]);
        pieces = splitTokens(s2, \" \");
        if (pieces.length > 8) 
        {
           h    = int(pieces[0]);
           m    = int(pieces[1]);
           s    = int(pieces[2]);
           a    = int(pieces[$g2]);
        }

        t = h*3600+m*60+s;
        diff = a - data1[t];

        if(t > xstart && t < xend)
        {
              y = (float)diff/$s1;
              y = (y-floor)/(roof-floor)*gH;    
              x = (float)(t-xstart)/(xend - xstart)*gW;
              y = gH-y;
              line(ot,op,x,y);
              ot=x;op=y;
        }
    }
    //=================================================
  } // setup


  void draw() 
  {
  }
  ";

  fwrite($f,$content,strlen($content)); 
  fclose($f); 
  return($file);
}
//======================================================================
function createPDE_oneWire($id,$year,$month,$day,$w1,$w2,$w3,$w4,$w5)
{

  $inpFile = 'data-'.$id.'-'.$year.'-'.$month.'-'.$day.'.arh';
  $file = 'w1Graph-'.$id.'.pde';
  $f = fopen($file,'w'); 

  $content = "String[] lines;
  int gH = 400, gW = 1000;
  int i,temp,maxVal=0;
  int xstart=0;
  int xend=24*3600;
  float floor = 0.;
  float roof = 50.;
  int[] a = new int[6];
  int[] w = new int[6];
  float[] ot = new float[6];
  float[] op = new float[6];
  float[] red   = new float[6];
  float[] green = new float[6];
  float[] blue  = new float[6];
  int[] curves = new int[6];

  void setup() 
  {

     curves[1] = $w1;
     curves[2] = $w2;
     curves[3] = $w3;
     curves[4] = $w4;
     curves[5] = $w5;

     red[1] =   0.; green[1] =  50.; blue[1] =   0.;
     red[2] =   0.; green[2] =   0.; blue[2] =  50.;
     red[3] =  50.; green[3] =  50.; blue[3] =   0.;

     size(gW, gH);
     background(200);
     fill(255,0,0);
     stroke(150,150,150);

     // Vertical lines
     for(i=1;i<24;i++)
     {
      temp = i/24*gW;
      line(temp,gH,temp,15);
      text(i,temp,20);
     }

     // Horizontal lines
     for(i=1;i<=int((roof-floor)/5);i++)
     {
       float yp = float(i*5)/((roof-floor)/gH);
       yp=gH-yp;
       line(0,yp,gW,yp);
       text(i*5+floor,0,yp);
     }

     lines = loadStrings(\"$inpFile\");
     text(\"OneWire\",10,10);
     int j=0,k=0,h=0,m=0,s=0;
     int t=0,t_prev=0,w_prev=0;

     for(i=0;i<lines.length;i++)
     {
        String s2 = trim(lines[i]);
        String[] pieces = splitTokens(s2, \" \");
        if (pieces.length > 8) 
        {
           h    = int(pieces[0]);
           m    = int(pieces[1]);
           s    = int(pieces[2]);
           t_prev = t;
           t = h*3600+m*60+s;
           //a[0] = int(pieces[3]);
           //a[1] = int(pieces[4]);
           //a[2] = int(pieces[5]);
           //a[3] = int(pieces[6]);
           //a[4] = int(pieces[7]);
           //a[5] = int(pieces[8]);
           w[1] = int(pieces[9]);
           w[2] = int(pieces[10]);
           w[3] = int(pieces[11]);
           w[4] = int(pieces[12]);
           w[5] = int(pieces[13]);
           //w_prev = w[4];
           //w[4] = (w[2] - w[1])*10;
           //w[5] = (w[4]- w_prev)/(t - t_prev)*1000;
        }


        //println(t);
        for(k=1;k<=5;k++)
        {
         if(curves[k] == 1)
         {
           stroke(red[k],green[k],blue[k]);
           if(t > xstart && t < xend)
           {
              float y = w[k]/100.; 
              y = (y-floor)/(roof-floor)*gH;   
              float x = (float)(t-xstart)/(xend - xstart)*gW;
              y = gH-y;
              line(ot[k],op[k],x,y);
              ot[k]=x;op[k]=y;
           }
         }
        }
      }
  }


  void draw() 
  {
  }
  ";

  fwrite($f,$content,strlen($content)); 
  fclose($f); 
}
//======================================================================
function createPDE_diffOneWire($id,$year,$month,$day,$c1,$c2)
{

  $inpFile = 'data-'.$id.'-'.$year.'-'.$month.'-'.$day.'.arh';
  $file = 'w1Diff-'.$id.'.pde';
  $f = fopen($file,'w'); 

  $content = "String[] lines;
  int gH = 400, gW = 1000;
  int i,temp,maxVal=0;
  int xstart=0;
  int xend=24*3600;
  float floor = -10.;
  float roof = 50.;
  int[] a = new int[6];
  int[] w = new int[6];
  float[] ot = new float[6];
  float[] op = new float[6];
  float[] red   = new float[6];
  float[] green = new float[6];
  float[] blue  = new float[6];
  int[] curves = new int[6];

  void setup() 
  {

     curves[1] = 0;
     curves[2] = 0;
     curves[3] = 0;
     curves[4] = 1;
     curves[5] = 0;

     red[1] =   0.; green[1] =  50.; blue[1] =   0.;
     red[2] =   0.; green[2] =   0.; blue[2] =  50.;
     red[3] =  50.; green[3] =  50.; blue[3] =   0.;

     size(gW, gH);
     background(200);
     fill(255,0,0);
     stroke(150,150,150);

     // Vertical lines
     for(i=1;i<24;i++)
     {
      temp = i/24*gW;
      line(temp,gH,temp,15);
      text(i,temp,20);
     }

     // Horizontal lines

     for(i=1;i<=int((roof-floor)/10);i++)
     {
       float yp = float(i*10)/((roof-floor)/gH);
       yp=gH-yp;
       line(0,yp,gW,yp);

       text(i*10+floor,0,yp);
     }

     lines = loadStrings(\"$inpFile\");

     text(\"DiffOneWire\",10,10);
     int j=0,k=0,h=0,m=0,s=0;
     int t=0,t_prev=0,w_prev=0;

     for(i=0;i<lines.length;i++)

     {
        String s2 = trim(lines[i]);
        String[] pieces = splitTokens(s2, \" \");
        if (pieces.length > 8) 
        {

           h    = int(pieces[0]);
           m    = int(pieces[1]);
           s    = int(pieces[2]);
           t_prev = t;

           t = h*3600+m*60+s;
           //a[0] = int(pieces[3]);
           //a[1] = int(pieces[4]);
           //a[2] = int(pieces[5]);
           //a[3] = int(pieces[6]);

           //a[4] = int(pieces[7]);
           //a[5] = int(pieces[8]);
           w[1] = int(pieces[9]);
           w[2] = int(pieces[10]);
           w[3] = int(pieces[11]);

           w_prev = w[4];
           w[4] = (w[$c1] - w[$c2])*10;
           w[5] = (w[4]- w_prev)/(t - t_prev)*1000;
        }



        //println(t);
        for(k=4;k<=5;k++)
        {

         if(curves[k] == 1)
         {
           stroke(red[k],green[k],blue[k]);
           if(t > xstart && t < xend)
           {

              float y = w[k]/100.; 
              y = (y-floor)/(roof-floor)*gH;   
              float x = (float)(t-xstart)/(xend - xstart)*gW;
              y = gH-y;

              line(ot[k],op[k],x,y);
              ot[k]=x;op[k]=y;
           }
         }
        }

      }
  }


  void draw() 

  {
  }
  ";

  fwrite($f,$content,strlen($content)); 
  fclose($f); 
}
//======================================================================
function createPDE_diffAnalog($id,$year,$month,$day,$chan1,$chan2)
{

  $inpFile = 'data-'.$id.'-'.$year.'-'.$month.'-'.$day.'.arh';

  $file = 'analogDiff-'.$id.'.pde';

  $f = fopen($file,'w'); 

  $content = "String[] lines;
  int gH = 200, gW = 1000;
  int i,temp,maxVal=0;
  int xstart=0;
  int xend=24*3600;
  float floor = 0.;
  float roof = 100.;
  int[] a = new int[6];
  float[] ot = new float[6];
  float[] op = new float[6];
  float[] red   = new float[6];
  float[] green = new float[6];
  float[] blue  = new float[6];
  float ftemp;

  void setup() 
  {

     red[0] =   0.; green[0] =   0.; blue[0] =   0.;
     red[1] =   0.; green[1] =  50.; blue[1] =   0.;
     red[2] =   0.; green[2] =   0.; blue[2] =  50.;
     red[3] =  50.; green[3] =  50.; blue[3] =   0.;
     red[4] =   0.; green[4] =  50.; blue[4] =  50.;
     red[5] =  50.; green[5] =  50.; blue[5] =  50.;

     size(gW, gH);
     background(200);
     fill(255,0,0);
     stroke(150,150,150);

     // Vertical lines
     for(i=1;i<24;i++)
     {
      temp = i/24*gW;
      line(temp,gH,temp,15);
      text(i,temp,20);
     }

     // Horizontal lines
     for(i=1;i<=int((roof-floor)/10);i++)
     {
       float yp = float(i*10)/((roof-floor)/gH);
       yp=gH-yp;
       line(0,yp,gW,yp);
       text(i*10+floor,0,yp);
     }

     lines = loadStrings(\"$inpFile\");
     text(\"DiffAnalog\",10,10);
     int j=0,k=0,h=0,m=0,s=0;
     int t=0,t_prev=0;

     for(i=0;i<lines.length;i++)
     {
        String s2 = trim(lines[i]);
        String[] pieces = splitTokens(s2, \" \");
        if (pieces.length > 8 ) 
        {
           h    = int(pieces[0]);
           m    = int(pieces[1]);
           s    = int(pieces[2]);
           a[0] = int(pieces[3]);
           a[1] = int(pieces[4]);
           a[2] = int(pieces[5]);
           a[3] = int(pieces[6]);
           a[4] = int(pieces[7]);
           a[5] = int(pieces[8]);
        }

        t_prev = t;
        t = h*3600+m*60+s;
        ftemp = a[$chan1]-a[$chan2];
           stroke(red[0],green[0],blue[0]);
           if(t > xstart && t < xend)
           {
              float y = (ftemp-floor)/(roof-floor)*gH;    
              float x = (float)(t-xstart)/(xend - xstart)*gW;
              y = gH-y;
              line(ot[k],op[k],x,y);
              ot[k]=x;op[k]=y;
           }

      }
  }


  void draw() 
  {
  }
  ";

  fwrite($f,$content,strlen($content)); 
  fclose($f); 
}

//============================
function createPDE_analogGraphNow($id,$a0,$a1,$a2,$a3,$a4,$a5)
{
  $file = 'analogGraphNow-'.$id.'.pde';
  $f = fopen($file,'w'); 

  $c_width  = 800;
  $c_height = 300;
  $c_scale  = 10;
  $c_ymax   = 1023;
  $content = "String[] lines;
int i,j,temp=0,maxVal=0,scaleVal=0,p;
int[] ot = new int[6];
int[] op = new int[6];
int xstart,xend;
int h,m,s,t,old_t=0;
int x,y,ftemp;
int window = 200;
int center = $c_width;
int nbuff = 1000;
int[] tBuff = new int[1001];
int[] pBuff = new int[6][1001];
int[] curves = new int[6];


int maxValue(int pin)
{
  int power=0.,maxPower=0.;
  int temp=0;

  for(i=nbuff;i>0;i--)
  {
     power = pBuff[pin][i];
     if(maxPower < power)maxPower = power;
  }

  for(i=0;i<100;i++)
  {
     if(maxPower > i*1000) temp = (i+1)*1000; 
  }
  return(temp);
}

int getScale(int value)
{
  int res = 50;
  if(value > 500)   res = 100;
  if(value > 1000)  res = 200;
  if(value > 2000)  res = 250;
  if(value > 4000)  res = 500;
  if(value > 10000) res = 1000;
  if(value > 20000) res = 2000;
  if(value > 50000) res = 10000;
  return(res);
}


void setup() {
  size($c_width, $c_height);
  background(200);
  fill(255,0,0);
  stroke(150,150,150);
  lines = loadStrings(\"now-$id.arh\");
  if(lines != null)
  {  
     h = int(lines[3]);
     m = int(lines[4]);
     s = int(lines[5]);
     t = h*3600+m*60+s;
  }
  xstart = t-window;
  xend   = t+window;
  for(i=0;i<=nbuff;i++)
  {
      for(j=0;j<6;j++)pBuff[j][i] = 0;
      tBuff[i] = 0;
  }
  curves[0] = $a0;
  curves[1] = $a1;
  curves[2] = $a2;
  curves[3] = $a3;
  curves[4] = $a4;
  curves[5] = $a5;
}

float xx=1,yy=5;
void draw() 
{
  int tt,a0,a1,a2,a3,a4,a5;
  stroke(0,0,0);
  lines = loadStrings(\"now-$id.arh\");
  if(lines != null)
  {  
    h = int(lines[3]);
    m = int(lines[4]);
    s = int(lines[5]);
    a0 = int(lines[6]);
    a1 = int(lines[7]);
    a2 = int(lines[8]);
    a3 = int(lines[9]);
    a4 = int(lines[10]);
    a5 = int(lines[11]);
    tt = h*3600+m*60+s;
  } 
 
  xx++;if(xx>1000){xx=1;}
  point(xx,yy);

  if(old_t < tt)
  {

    for(i=nbuff;i>0;i--)
    {
      tBuff[i] = tBuff[i-1];
      for(j=0;j<6;j++)pBuff[j][i] = pBuff[j][i-1];
    }
    tBuff[0] = tt;
    pBuff[0][0] = a0;
    pBuff[1][0] = a1;
    pBuff[2][0] = a2;
    pBuff[3][0] = a3;
    pBuff[4][0] = a4;
    pBuff[5][0] = a5;
//=====================================
  background(200);
  fill(255,0,0);
  stroke(150,150,150);
  // presentation 

  maxVal   = maxValue(0);
  scaleVal = getScale(maxVal);

  p = pBuff[0][0]/(maxVal/$c_height);    
  //ftemp = (float)(center)/(xend - xstart)*$c_width;
  p = $c_height-p;
  for(j=0;j<6;j++){ot[j] = center;op[j] = p;}
  //point(x,y);

   for(i=0;i<window;i+=10)
   {
    ftemp = (float)i/window*$c_width;
    line(ftemp,$c_height,ftemp,15);
    if(i%50 == 0)text(i,ftemp,20);
   }

  for(i=1;i<=int(maxVal/scaleVal);i++)
  {
    float yp = float(i*scaleVal)/(maxVal/$c_height);
    yp=$c_height-yp;
    line(0,yp,$c_width,yp);
    text(i*scaleVal,0,yp);
  }
//====================================
 
    for(j=0;j<6;j++)
    {
	  if(curves[j] == 1)
	  {
      //xx++;if(xx>1000){xx=1;}
      //point(xx,yy+10);
  
      stroke(j*50,0,0);
      for(i=1;i<=nbuff;i++)
      {
        // text(i,200,200);
        p = pBuff[j][i]; 
        int time = tBuff[0] - tBuff[i];

        if(p > 0 && time <= center)
        {

          // presentation 
 
          ftemp = time/(xend - xstart)*$c_width;
          x = center-ftemp;

          ftemp = p/(maxVal/$c_height);   
          y = $c_height-ftemp;

          line(ot[j],op[j],x,y);
          ot[j]=x;op[j]=y;
        }
       }
      }
     }
//====================================
  }
  old_t = tt;
}
";
  fwrite($f,$content,strlen($content)); 
  fclose($f); 
}

//============================
function writeCtrl($id,$command,$arg1,$arg2) 
{
  echo("Create Ctrl Command $id,$command,$arg1,$arg2<br>");
  $file = 'ctrl-'.$id.".arh"; 
  $out = fopen($file, 'w');
  $temp = $command.' '.$arg1.' '.$arg2;
  fwrite($out, $temp); 
  fclose($out); 
}
//============================
//  Arduino API Wrapper Library
//============================
function stepper($id,$dir,$steps) 
{
  writeCtrl($id,STEPPER,$dir,$steps);
}

function setHeartBeat($id,$value) 
{
  writeCtrl($id,HEARTBEAT,$value,"void");
}

function digitalWriteAll($id,$value) 
{
  writeCtrl($id,DIGWRIALL,"void",$value);
}

function digitalWrite($id,$pin,$value) 
{
  writeCtrl($id,DIGITALWRITE,$pin,$value);
}

function digitalRead($id,$pin) 
{
  global $g_digital;
  //writeCtrl($id,DIGITALREAD,$pin,0);
  return($g_digital[$id][$pin]);
}
function analogWrite($id,$pin,$value) // PWM
{
  writeCtrl($id,ANALOGWRITE,$pin,$value);
}
function analogRead($id,$pin) 
{
  global $g_analog;
  //writeCtrl($id,ANALOGREAD,$pin,0);
  return($g_analog[$id][$pin]);
}
function pinMode($id,$pin,$value) 
{
  writeCtrl($id,PINMODE,$pin,$value);
}
function pinModeAll($id,$value) 
{
  writeCtrl($id,PINMODALL,"void",$value);
}

function readOneWire($id,$sensor) 
{
  global $g_oneWire;
  getStatus($id); 
  $temp = $g_oneWire[$id][$sensor];
  return($temp);
}

?>
